
// #include "../lvgl/lvgl.h"
// #include "../lvgl/demos/lv_demos.h"

// #include "../lv_drivers/display/fbdev.h"
// #include "../lv_drivers/indev/evdev.h"
#include "ui.h"
#include "snake.h"
#include "snake_data.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int game_score = 0;
int snake_eat_apple_flag = 0;
int rank_init_flag = 0; 


void * lv_mem_alloc(size_t size) {
    return lv_malloc(size);
}

void lv_mem_free(void * ptr) {
    lv_free(ptr);
}

// 坐标映射
int xy_to_num(int x){
    return x*40;
}

// 角度映射
int direction_to_angle(int direction){
    int angle = 0;
    switch(direction){
        case DIRECTION_LEFT:
            angle = ANGLE_LEFT;
            break;
        case DIRECTION_RIGHT:
            angle = ANGLE_RIGHT;
            break;
        case DIRECTION_UP:
            angle = ANGLE_UP;
            break;
        case DIRECTION_DOWN:
            angle = ANGLE_DOWN;
            break;
        default: break;
    }
    return angle;
}

// 游戏初始化
snake_head_p snake_init(){
    // 蛇头对象节点初始化
    srand(time(0));
    snake_head_p snake_head = (snake_head_p)lv_mem_alloc(sizeof(snake_head_t));
    snake_head->snake_head_angle = DIRECTION_LEFT;

    for(int i = 0; i < SNAKE_BODY_LEN; i++){
        snake_body_arr[i] = NULL;
    }
    for(int i = 0; i < SNAKE_BOMB_MAX; i++){
        snake_bomb_arr[i] = NULL;
    }
    for(int i = 0; i < SNAKE_APPLE_MAX; i++){
        snake_apple_arr[i] = NULL;
    }

    // 蛇头初始化
    // 对象初始化  
    snake_head->snake_head = lv_img_create(ui_screenGame);
    snake_body_arr[0] = snake_head->snake_head;
    lv_img_set_src(snake_head->snake_head, &ui_img_head_png);
    lv_obj_set_size(snake_head->snake_head, 40, 40);        // 设置尺寸
    lv_obj_set_x(snake_head->snake_head, xy_to_num(init_head_x));
    lv_obj_set_y(snake_head->snake_head, xy_to_num(init_head_y));
    // 相关参数初始化（方向，坐标）
    snake_head->snake_head_angle = DIRECTION_RIGHT;
    snake_head->snake_body_x_arr[0] = init_head_x;
    snake_head->snake_body_y_arr[0] = init_head_y;

    /* 蛇身初始化 */
    // 对象初始化
    snake_body_arr[1] = lv_img_create(ui_screenGame);
    lv_img_set_src(snake_body_arr[1], &ui_img_body_png);
    lv_obj_set_size(snake_body_arr[1], 40, 40);        // 设置尺寸
    lv_obj_set_x(snake_body_arr[1], xy_to_num(init_head_x-1));
    lv_obj_set_y(snake_body_arr[1], xy_to_num(init_head_y));
    // 相关参数初始化（类型，方向，坐标）
    snake_head->snake_body_type[1] = SNAKE_BODY_TYPE1;
    snake_head->snake_body_direction[1] = DIRECTION_RIGHT;
    snake_head->snake_body_x_arr[1] = init_head_x-1;
    snake_head->snake_body_y_arr[1] = init_head_y;

    // 蛇尾初始化
    snake_body_arr[SNAKE_TAIL_INDEX] = lv_img_create(ui_screenGame);
    lv_img_set_src(snake_body_arr[SNAKE_TAIL_INDEX], &ui_img_tail_png);
    lv_obj_set_size(snake_body_arr[SNAKE_TAIL_INDEX], 40, 40);        // 设置尺寸
    lv_obj_set_x(snake_body_arr[SNAKE_TAIL_INDEX], xy_to_num(init_head_x-2));
    lv_obj_set_y(snake_body_arr[SNAKE_TAIL_INDEX], xy_to_num(init_head_y));
    // 蛇尾相关参数初始化（方向，编号，坐标）
    snake_head->snake_tail_direction = DIRECTION_RIGHT;
    snake_head->snake_tail_number = SNAKE_TAIL_INDEX;
    snake_head->snake_body_x_arr[SNAKE_TAIL_INDEX] = init_head_x-2;
    snake_head->snake_body_y_arr[SNAKE_TAIL_INDEX] = init_head_y;

    // 蛇身长度初始化
    snake_head->snake_length = 3;

    // 蛇身队列初始化
    snake_head->snake_body_queue_head = 1;
    snake_head->snake_body_queue_tail = 1;
    snake_head->snake_body_queue_len = 0;
    snake_body_queue_enqueue(snake_head, 1);

    // 最后一节蛇身编号初始化
    snake_head->snake_lastbody_number = snake_body_queue_head_index(snake_head);

    // 食物初始化
    apple_create(snake_head);

    game_score = 0;
    snake_eat_apple_flag = 0;

    printf("2. game init success!\n");
    return snake_head;
}

// 游戏结算
void game_over(snake_head_p* snake_head_ptr) {
    // 安全检查：如果二级指针或其指向的一级指针为空，则直接返回
    if (snake_head_ptr == NULL || *snake_head_ptr == NULL) {
        printf("game_over: 蛇头指针无效，无需处理。\n");
        return;
    }

    snake_head_p head = *snake_head_ptr; // 获取一级指针以便操作
    game_score = head->snake_length - 3; // 得分计算
    char score_str[10];
    sprintf(score_str, "%d", game_score);
    lv_label_set_text(ui_overScoreText, score_str);

    printf("游戏结束！最终得分: %d\n", game_score); // 打印得分有助于调试

    // 删除所有蛇身部分（包括头、身体、尾）
    for (int i = 0; i < SNAKE_BODY_LEN; i++) {
        if (snake_body_arr[i] != NULL) {
            lv_obj_del(snake_body_arr[i]); // 使用LVGL的对象删除函数
            snake_body_arr[i] = NULL;      // 指针立即置NULL
        }
    }

    // 删除所有炸弹
    for (int i = 0; i < SNAKE_BOMB_MAX; i++) {
        if (snake_bomb_arr[i] != NULL) {
            lv_obj_del(snake_bomb_arr[i]);
            snake_bomb_arr[i] = NULL;
        }
    }

    // 删除所有食物
    for (int i = 0; i < SNAKE_APPLE_MAX; i++) {
        if (snake_apple_arr[i] != NULL) {
            lv_obj_del(snake_apple_arr[i]);
            snake_apple_arr[i] = NULL;
        }
    }

    // 3. 重置所有全局状态变量和计数器
    // game_score = 0;
    game_gesture_count_enum = game_gesture_over_count; // 重置方向输入
    game_data_save_enum = game_data_save_none; // 重置数据保存状态

    // 4. 释放蛇头结构体本身，并将主函数中的指针置为NULL
    lv_mem_free(head);
    *snake_head_ptr = NULL; // 至关重要！避免主函数中的悬空指针。

    // 5. 更新屏幕状态并切换到结束界面
    screen_state_enum = screen_over;
    _ui_screen_change(&ui_screenOver, LV_SCR_LOAD_ANIM_NONE, 0, 0, &ui_screenOver_screen_init);

    printf("2. game over success!\n");
}

// 分数和姓名保存
void game_save_score(){
    if(game_data_save_enum == game_data_save_ing){
        const char * input_text = lv_textarea_get_text(ui_overNameEnter);
        snake_data_add_line(input_text, game_score);
        game_data_save_enum = game_data_save_success;
        lv_textarea_set_text(ui_overNameEnter, "");
    }
}

// 初始化排名
void rank_init(){
    snake_data_head_p head = snake_data_open("./snake_data.txt");
    if(head == NULL){
        printf("rank_init: 排名数据文件打开失败，无法初始化排名。\n");
        return;
    }
    else{
        rank_init_flag = 1;
        printf("rank_init: 排名数据文件打开成功，开始初始化排名。\n");
    }
    // 排序
    snake_data_sort(head);

    // 格式化前五名
    char rank_str[300] = {0};
    snake_data_p node = head->head;
    int i = 0;
    while (node != NULL && i < 5) {
        printf("%d. %s:%d\n", i + 1, node->name, node->score);
        sprintf(rank_str + strlen(rank_str), "No.%d.   %s  :  %d\n", i + 1, node->name, node->score);
        node = node->next;
        i++;
    }
    lv_label_set_text(ui_Label4, rank_str);
}

// 碰撞检测函数
enum collision_type snake_collision_check(snake_head_p snake_head , int x , int y){
    // 蛇头坐标
    for (int i = 1; i < snake_head->snake_length; i++) {
        if(snake_head->snake_body_x_arr[i] == x && snake_head->snake_body_y_arr[i] == y){
            return collision_body;
        }
    }
    // 炸弹坐标
    for (int i = 0; i < snake_head->bomb_quantity; i++) {
        if(snake_head->snake_bomb_x_arr[i] == x && snake_head->snake_bomb_y_arr[i] == y){
            return collision_bomb;
        }
    }
    // 食物坐标
    for (int i = 0; i < snake_head->apple_quantity; i++) {
        if(snake_head->snake_apple_x_arr[i] == x && snake_head->snake_apple_y_arr[i] == y){
            return collision_apple;
        }
    }
    return collision_none;
}

// 蛇移动
void snake_move(snake_head_p snake_head){
    // if(game_gesture_count_enum == game_gesture_over_count){
    //     return;
    // }

    int x_head = snake_head->snake_body_x_arr[0];
    int y_head = snake_head->snake_body_y_arr[0];
    enum collision_type collision = collision_none;
    // 移动方向获取

    switch(game_gesture_count_enum){
        case game_gesture_left_count:
            x_head -= 1;
            break;
        case game_gesture_right_count:
            x_head += 1;
            break;
        case game_gesture_up_count:
            y_head -= 1;
            break;
        case game_gesture_down_count:
            y_head += 1;
            break;
        case game_gesture_over_count:
            switch(snake_head->snake_head_angle){
                case DIRECTION_UP:
                    y_head -= 1;
                    break;
                case DIRECTION_DOWN:
                    y_head += 1;
                    break;
                case DIRECTION_LEFT:
                    x_head -= 1;
                    break;
                case DIRECTION_RIGHT:
                    x_head += 1;
                    break;
                default: break;
            }
        default: break;
    }

    enum move_direction move_direction = snake_move_direction_get(snake_head , x_head , y_head);
    if (move_direction == move_back) {
        // 方向错误，直线移动
        x_head = snake_head->snake_body_x_arr[0];
        y_head = snake_head->snake_body_y_arr[0];
        game_gesture_count_enum = game_gesture_over_count;
        switch(snake_head->snake_head_angle){
            case DIRECTION_UP:
                y_head -= 1;
                break;
            case DIRECTION_DOWN:
                y_head += 1;
                break;
            case DIRECTION_LEFT:
                x_head -= 1;
                break;
            case DIRECTION_RIGHT:
                x_head += 1;
                break;
            default: break;
        }        
    }

    // 边界处理
    if(x_head < 0)x_head = 19;
    if(x_head >= 20)x_head = 0;
    if(y_head < 0)y_head = 11;
    if(y_head >= 12)y_head = 0;

    // 蛇身满了，游戏结束
    if(snake_head->snake_length == SNAKE_BODY_LEN - 1){
        game_over(&snake_head);
        return;
    }

    // 碰撞处理
    collision = snake_collision_check(snake_head , x_head , y_head);
    switch(collision){
        case collision_body:
            // 蛇头撞到蛇身
            game_over(&snake_head);
            return;
            break;
        case collision_bomb:
            // 蛇头撞到炸弹
            game_over(&snake_head);
            return;
            break;
        case collision_apple:
            // 蛇头吃到食物
            printf("have eat food!\n");
            snake_eat_apple(snake_head , x_head , y_head);
            snake_eat_apple_flag = 1;
            break;
        default: break;
    }

    // 蛇身坐标数组更新
    snake_move_direction(snake_head , x_head , y_head); //实际移动

    // 清空枚举标记
    snake_eat_apple_flag = 0;
    game_gesture_count_enum = game_gesture_over_count;
}

// 获取移动方向
enum move_direction snake_move_direction_get(snake_head_p snake_head , int x_head , int y_head){
    enum move_direction move_direction = move_straight;
    if(snake_head->snake_head_angle == game_gesture_count_enum){
        move_direction = move_straight;
    }
    else if (snake_head->snake_head_angle == DIRECTION_RIGHT && game_gesture_count_enum == DIRECTION_UP){
        move_direction = move_turen_left;
    }
    else if (snake_head->snake_head_angle == DIRECTION_RIGHT && game_gesture_count_enum == DIRECTION_DOWN){
        move_direction = move_turen_right;
    }
    else if (snake_head->snake_head_angle == DIRECTION_LEFT && game_gesture_count_enum == DIRECTION_UP){
        move_direction = move_turen_right;
    }
    else if (snake_head->snake_head_angle == DIRECTION_LEFT && game_gesture_count_enum == DIRECTION_DOWN){
        move_direction = move_turen_left;
    }
    else if (snake_head->snake_head_angle == DIRECTION_UP && game_gesture_count_enum == DIRECTION_LEFT){
        move_direction = move_turen_left;
    }
    else if (snake_head->snake_head_angle == DIRECTION_UP && game_gesture_count_enum == DIRECTION_RIGHT){
        move_direction = move_turen_right;
    }
    else if (snake_head->snake_head_angle == DIRECTION_DOWN && game_gesture_count_enum == DIRECTION_LEFT){
        move_direction = move_turen_right;
    }
    else if (snake_head->snake_head_angle == DIRECTION_DOWN && game_gesture_count_enum == DIRECTION_RIGHT){
        move_direction = move_turen_left;
    }
    else if (snake_head->snake_head_angle == DIRECTION_RIGHT && game_gesture_count_enum == DIRECTION_LEFT){
        move_direction = move_back;
    }
    else if (snake_head->snake_head_angle == DIRECTION_LEFT && game_gesture_count_enum == DIRECTION_RIGHT){
        move_direction = move_back;
    }
    else if (snake_head->snake_head_angle == DIRECTION_UP && game_gesture_count_enum == DIRECTION_DOWN){
        move_direction = move_back;
    }
    else if (snake_head->snake_head_angle == DIRECTION_DOWN && game_gesture_count_enum == DIRECTION_UP){
        move_direction = move_back;
    }
    else{
        move_direction = move_straight;
    }
    return move_direction;

}

// 贪吃蛇移动方向处理
void snake_move_direction (snake_head_p snake_head , int x_head , int y_head){
    enum move_direction move_direction = snake_move_direction_get(snake_head , x_head , y_head);

    // 移动处理
    switch(move_direction){
        case move_straight:
            snake_move_straight(snake_head , x_head , y_head);
            break;
        case move_turen_left:
            //printf("1.turen_left , %d\n" , snake_head->snake_head_angle);
            snake_move_turen_left(snake_head , x_head , y_head);
            break;
        case move_turen_right:
            //printf("1.turen_right , %d\n" , snake_head->snake_head_angle);
            snake_move_turen_right(snake_head , x_head , y_head);
            break;
        default: break;
    }
}

// 贪吃蛇队列入队
void snake_body_queue_enqueue(snake_head_p snake_head , int index){
    // 判断队列是否已满
    if(snake_head->snake_body_queue_len == SNAKE_BODY_LEN - 1){
        return;
    }
    // 队列尾部入队
    snake_head->snake_body_queue_arr[snake_head->snake_body_queue_tail] = index;
    snake_head->snake_body_queue_tail = (snake_head->snake_body_queue_tail + 1) % SNAKE_BODY_LEN;
    snake_head->snake_body_queue_len++;
}

// 贪吃蛇队列出队
int snake_body_queue_dequeue(snake_head_p snake_head){
    int index = -1;
    if(snake_head->snake_body_queue_len > 0){
        index = snake_head->snake_body_queue_arr[snake_head->snake_body_queue_head];
        snake_head->snake_body_queue_head = (snake_head->snake_body_queue_head + 1) % SNAKE_BODY_LEN;
        snake_head->snake_body_queue_len--;
    }
    return index;
}

// 获取队首编号
int snake_body_queue_head_index(snake_head_p snake_head){
    int index = -1;
    if(snake_head->snake_body_queue_len > 0){
        index = snake_head->snake_body_queue_arr[snake_head->snake_body_queue_head];
    }
    return index;
}

// 直线移动处理
void snake_move_straight(snake_head_p snake_head , int x_head , int y_head){
    // 保存蛇头坐标
    int temp_x  = snake_head->snake_body_x_arr[0];
    int temp_y  = snake_head->snake_body_y_arr[0];
    // 蛇头坐标更新
    snake_head->snake_body_x_arr[0] = x_head;
    snake_head->snake_body_y_arr[0] = y_head;

    // 蛇头图像移动
    snake_head_img_move(snake_head);

    if(snake_eat_apple_flag == 0){

        // 蛇尾坐标等于最后一节蛇身坐标
        snake_head->snake_body_x_arr[snake_head->snake_tail_number] = snake_head->snake_body_x_arr[snake_head->snake_lastbody_number];
        snake_head->snake_body_y_arr[snake_head->snake_tail_number] = snake_head->snake_body_y_arr[snake_head->snake_lastbody_number];
        // 蛇尾方向等于最后一节蛇身方向
        snake_head->snake_tail_direction = snake_head->snake_body_direction[snake_head->snake_lastbody_number];

        // 蛇尾图像移动
        snake_tail_img_move(snake_head);    

        /*最后一节蛇身移动到蛇头位置*/
        /*蛇身类型 ， 蛇身方向 ， 蛇身坐标数组更新*/
        // 蛇身类型数组更新，直线类型
        snake_head->snake_body_type[snake_head->snake_lastbody_number] = SNAKE_BODY_TYPE1;
        // 蛇身方向数组更新
        snake_head->snake_body_direction[snake_head->snake_lastbody_number] = snake_head->snake_head_angle;
        // 最后一节蛇身坐标等于蛇头坐标
        snake_head->snake_body_x_arr[snake_head->snake_lastbody_number] = temp_x;
        snake_head->snake_body_y_arr[snake_head->snake_lastbody_number] = temp_y;

        // 最后一节蛇身图像对象移动
        snake_body_img_move(snake_head , snake_head->snake_lastbody_number);   

        // 队列索引更新
        int temp_index = snake_body_queue_dequeue(snake_head);
        snake_body_queue_enqueue(snake_head , temp_index);

        // 最后一节蛇身索引更新
        snake_head->snake_lastbody_number = snake_body_queue_head_index(snake_head);

    }
    snake_eat_apple_flag = 0;
}

// 左转移动处理
void snake_move_turen_left(snake_head_p snake_head , int x_head , int y_head){
    // 保存蛇头坐标
    int temp_x  = snake_head->snake_body_x_arr[0];
    int temp_y  = snake_head->snake_body_y_arr[0];
    // 蛇头坐标更新
    snake_head->snake_body_x_arr[0] = x_head;
    snake_head->snake_body_y_arr[0] = y_head;
    // 蛇头方向设置
    snake_head->snake_head_angle = game_gesture_count_enum;
    // 蛇头图像移动
    snake_head_img_move(snake_head);

    // 蛇尾坐标等于最后一节蛇身坐标
    snake_head->snake_body_x_arr[snake_head->snake_tail_number] = snake_head->snake_body_x_arr[snake_head->snake_lastbody_number];
    snake_head->snake_body_y_arr[snake_head->snake_tail_number] = snake_head->snake_body_y_arr[snake_head->snake_lastbody_number];
    // 蛇尾方向等于最后一节蛇身方向
    snake_head->snake_tail_direction = snake_head->snake_body_direction[snake_head->snake_lastbody_number];

    // 蛇尾图像移动
    snake_tail_img_move(snake_head);    

    // 蛇身类型数组更新，左转类型
    snake_head->snake_body_type[snake_head->snake_lastbody_number] = SNAKE_BODY_TYPE2;
    // 蛇身方向数组更新
    snake_head->snake_body_direction[snake_head->snake_lastbody_number] = snake_head->snake_head_angle;
    // 最后一节蛇身坐标等于蛇头坐标
    snake_head->snake_body_x_arr[snake_head->snake_lastbody_number] = temp_x;
    snake_head->snake_body_y_arr[snake_head->snake_lastbody_number] = temp_y;

    // 最后一节蛇身图像对象移动
    snake_body_img_move_left(snake_head , snake_head->snake_lastbody_number);  

    // 队列索引更新
    int temp_index = snake_body_queue_dequeue(snake_head);
    snake_body_queue_enqueue(snake_head , temp_index);

    // 最后一节蛇身索引更新
    snake_head->snake_lastbody_number = snake_body_queue_head_index(snake_head);
}

// 右转移动处理
void snake_move_turen_right(snake_head_p snake_head , int x_head , int y_head){
    // 保存蛇头坐标
    int temp_x  = snake_head->snake_body_x_arr[0];
    int temp_y  = snake_head->snake_body_y_arr[0];
    // 蛇头坐标更新
    snake_head->snake_body_x_arr[0] = x_head;
    snake_head->snake_body_y_arr[0] = y_head;
    // 蛇头方向设置
    snake_head->snake_head_angle = game_gesture_count_enum;
    // 蛇头图像移动
    snake_head_img_move(snake_head);

    // 蛇尾坐标等于最后一节蛇身坐标
    snake_head->snake_body_x_arr[snake_head->snake_tail_number] = snake_head->snake_body_x_arr[snake_head->snake_lastbody_number];
    snake_head->snake_body_y_arr[snake_head->snake_tail_number] = snake_head->snake_body_y_arr[snake_head->snake_lastbody_number];
    // 蛇尾方向等于最后一节蛇身方向
    snake_head->snake_tail_direction = snake_head->snake_body_direction[snake_head->snake_lastbody_number];

    // 蛇尾图像移动
    snake_tail_img_move(snake_head);    

    // 蛇身类型数组更新，右转类型
    snake_head->snake_body_type[snake_head->snake_lastbody_number] = SNAKE_BODY_TYPE2;
    // 蛇身方向数组更新
    snake_head->snake_body_direction[snake_head->snake_lastbody_number] = snake_head->snake_head_angle;
    // 最后一节蛇身坐标等于蛇头坐标
    snake_head->snake_body_x_arr[snake_head->snake_lastbody_number] = temp_x;
    snake_head->snake_body_y_arr[snake_head->snake_lastbody_number] = temp_y;

    // 最后一节蛇身图像对象移动
    snake_body_img_move(snake_head , snake_head->snake_lastbody_number);   

    // 队列索引更新
    int temp_index = snake_body_queue_dequeue(snake_head);
    snake_body_queue_enqueue(snake_head , temp_index);

    // 最后一节蛇身索引更新
    snake_head->snake_lastbody_number = snake_body_queue_head_index(snake_head);

}

// 蛇头图像移动
void snake_head_img_move(snake_head_p snake_head){
    // 蛇头图像移动
    lv_obj_set_x(snake_head->snake_head, xy_to_num(snake_head->snake_body_x_arr[0]));
    lv_obj_set_y(snake_head->snake_head, xy_to_num(snake_head->snake_body_y_arr[0]));
    // 蛇头方向设置

    switch(snake_head->snake_head_angle){
        case DIRECTION_LEFT:
            lv_img_set_angle(snake_head->snake_head, ANGLE_LEFT);
            break;
        case DIRECTION_RIGHT:
            lv_img_set_angle(snake_head->snake_head, ANGLE_RIGHT);
            break;
        case DIRECTION_UP:
            lv_img_set_angle(snake_head->snake_head, ANGLE_UP);
            break;
        case DIRECTION_DOWN:
            lv_img_set_angle(snake_head->snake_head, ANGLE_DOWN);
            break;
        default: break;
    }
}

// 蛇尾图像移动
void snake_tail_img_move(snake_head_p snake_head){
    // 蛇尾图像移动
    lv_obj_set_x(snake_body_arr[snake_head->snake_tail_number], xy_to_num(snake_head->snake_body_x_arr[snake_head->snake_tail_number]));
    lv_obj_set_y(snake_body_arr[snake_head->snake_tail_number], xy_to_num(snake_head->snake_body_y_arr[snake_head->snake_tail_number]));
    // 蛇尾方向设置
    switch(snake_head->snake_tail_direction){
        case DIRECTION_LEFT:
            lv_img_set_angle(snake_body_arr[snake_head->snake_tail_number], ANGLE_LEFT);
            break;
        case DIRECTION_RIGHT:
            lv_img_set_angle(snake_body_arr[snake_head->snake_tail_number], ANGLE_RIGHT);
            break;
        case DIRECTION_UP:
            lv_img_set_angle(snake_body_arr[snake_head->snake_tail_number], ANGLE_UP);
            break;
        case DIRECTION_DOWN:
            lv_img_set_angle(snake_body_arr[snake_head->snake_tail_number], ANGLE_DOWN);
            break;
        default: break;
    }
}

// 蛇身图像移动
void snake_body_img_move(snake_head_p snake_head  , int index){
    // 蛇身图像移动
    lv_obj_set_x(snake_body_arr[index], xy_to_num(snake_head->snake_body_x_arr[index]));
    lv_obj_set_y(snake_body_arr[index], xy_to_num(snake_head->snake_body_y_arr[index]));
    // 蛇身类型设置
    switch(snake_head->snake_body_type[index]){
        case SNAKE_BODY_TYPE1:
            lv_img_set_src(snake_body_arr[index], &ui_img_body_png);
            break;
        case SNAKE_BODY_TYPE2:
            lv_img_set_src(snake_body_arr[index], &ui_img_body2_png);
            break;
        default: break;
    }
    // 蛇身方向设置
    switch(snake_head->snake_body_direction[index]){
        case DIRECTION_LEFT:
            lv_img_set_angle(snake_body_arr[index], ANGLE_LEFT);
            break;
        case DIRECTION_RIGHT:
            lv_img_set_angle(snake_body_arr[index], ANGLE_RIGHT);
            break;
        case DIRECTION_UP:
            lv_img_set_angle(snake_body_arr[index], ANGLE_UP);
            break;
        case DIRECTION_DOWN:
            lv_img_set_angle(snake_body_arr[index], ANGLE_DOWN);
            break;
        default: break;
    }
}

// 蛇身图像左转移动
void snake_body_img_move_left(snake_head_p snake_head  , int index){
    // 蛇身图像移动
    lv_obj_set_x(snake_body_arr[index], xy_to_num(snake_head->snake_body_x_arr[index]));
    lv_obj_set_y(snake_body_arr[index], xy_to_num(snake_head->snake_body_y_arr[index]));
    // 蛇身类型设置
    switch(snake_head->snake_body_type[index]){
        case SNAKE_BODY_TYPE1:
            lv_img_set_src(snake_body_arr[index], &ui_img_body_png);
            break;
        case SNAKE_BODY_TYPE2:
            lv_img_set_src(snake_body_arr[index], &ui_img_body2_png);
            break;
        default: break;
    }
    // 蛇身方向设置
    switch(snake_head->snake_body_direction[index]){
        case DIRECTION_LEFT:
            lv_img_set_angle(snake_body_arr[index], ANGLE_DOWN);
            break;
        case DIRECTION_RIGHT:
            lv_img_set_angle(snake_body_arr[index], ANGLE_UP);
            break;
        case DIRECTION_UP:
            lv_img_set_angle(snake_body_arr[index], ANGLE_LEFT);
            break;
        case DIRECTION_DOWN:
            lv_img_set_angle(snake_body_arr[index], ANGLE_RIGHT);
            break;
        default: break;
    }
}

// 蛇头吃到食物
void snake_eat_apple(snake_head_p snake_head,int x_head ,int y_head){
    enum move_direction move_direction = snake_move_direction_get(snake_head , x_head , y_head);
    // 对象
    int new_body_index = snake_head->snake_body_queue_len + 1;  
    snake_body_arr[new_body_index] = lv_img_create(ui_screenGame);
    lv_obj_set_size(snake_body_arr[new_body_index], 40, 40);        // 设置尺寸    

    // 相关参数更新（类型，方向，坐标）
    // 直接等于蛇头就好了

    snake_head->snake_body_x_arr[new_body_index] = snake_head->snake_body_x_arr[0];
    snake_head->snake_body_y_arr[new_body_index] = snake_head->snake_body_y_arr[0];

    switch(move_direction){
        case move_straight:
            snake_head->snake_body_direction[new_body_index] = snake_head->snake_head_angle;
            snake_head->snake_body_type[new_body_index] = SNAKE_BODY_TYPE1;
            lv_img_set_src(snake_body_arr[new_body_index], &ui_img_body_png);
            lv_obj_set_x(snake_body_arr[new_body_index], xy_to_num(snake_head->snake_body_x_arr[0]));
            lv_obj_set_y(snake_body_arr[new_body_index], xy_to_num(snake_head->snake_body_y_arr[0]));
            lv_img_set_angle(snake_body_arr[new_body_index], direction_to_angle(snake_head->snake_body_direction[new_body_index]));
            break;
        case move_turen_left:
            snake_head->snake_body_direction[new_body_index] = game_gesture_count_enum;
            snake_head->snake_body_type[new_body_index] = SNAKE_BODY_TYPE2;
            snake_body_img_move_left(snake_head , new_body_index);
            break;
        case move_turen_right:
            snake_head->snake_body_direction[new_body_index] = game_gesture_count_enum;
            snake_head->snake_body_type[new_body_index] = SNAKE_BODY_TYPE2;
            snake_body_img_move(snake_head , new_body_index);
            break;
        default: break;
    }

    // 蛇身长度增加
    snake_head->snake_length++;

    // 入队
    snake_body_queue_enqueue(snake_head , new_body_index);

    apple_move(snake_head);
}

// 食物刷新检测
char apple_creat_check(snake_head_p snake_head , int x , int y){
    for (int i = 0; i < snake_head->snake_length; i++) {
        if(snake_head->snake_body_x_arr[i] == x && snake_head->snake_body_y_arr[i] == y){
            return 1;
        }
    }
    return 0;
}

// 食物创建
void apple_create(snake_head_p snake_head){
    // 随机生成坐标
    int x ,y;
    do{
        x = rand() % 20;
        y = rand() % 12;
    }
    while (apple_creat_check(snake_head , x , y));

    // 随机生成图像
    snake_apple_arr[0] = lv_img_create(ui_screenGame);
    lv_img_set_src(snake_apple_arr[0], &ui_img_apple_png);
    lv_obj_set_size(snake_apple_arr[0], 40, 40);        
    lv_obj_set_x(snake_apple_arr[0], xy_to_num(x));
    lv_obj_set_y(snake_apple_arr[0], xy_to_num(y));

    snake_head->snake_apple_x_arr[0] = x;
    snake_head->snake_apple_y_arr[0] = y;
    snake_head->apple_quantity = 1;
}

// 食物移动
void apple_move(snake_head_p snake_head){
    // 随机生成坐标
    int x = rand() % 20;
    int y = rand() % 12;

    lv_obj_set_x(snake_apple_arr[0], xy_to_num(x));
    lv_obj_set_y(snake_apple_arr[0], xy_to_num(y));
    snake_head->snake_apple_x_arr[0] = x;
    snake_head->snake_apple_y_arr[0] = y;

}
